package com.bk.bookviews.controller;

import com.bk.bookviews.common.Pagination;
import com.bk.bookviews.domain.BookingOrder;
import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.exception.BusinessExceptionEnum;
import com.bk.bookviews.service.BookingOrderService;
import com.bk.bookviews.util.PageUtils;
import com.bk.bookviews.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bk/order")
public class BookingOrderController {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    private BookingOrderService orderService;
    @PostMapping("/add")
    public ResultMsg saveOrder(@RequestBody BookingOrder bookingOrder, @RequestHeader("token") String token){
        if (bookingOrder.getScenicId() == null){
            throw new BusinessException(BusinessExceptionEnum.SCENIC_ID_IS_NULL);
        }
        if (bookingOrder.getBookingTime() == null ){
            throw new BusinessException(BusinessExceptionEnum.BOOK_TIME_ISNULL);
        }
        orderService.saveOrder(bookingOrder,token);
        return ResultMsg.successResult();
    }
    @PutMapping("/update")
    public ResultMsg updateOrder(@RequestBody BookingOrder bookingOrder) {
        orderService.updateOrder(bookingOrder);
        return ResultMsg.successResult();
    }

    @GetMapping("/detail")
    public ResultMsg selectOrder(String orderId, @RequestHeader("token") String token){
        return ResultMsg.successResult(orderService.selectOrder(orderId,token));
    }

    @PostMapping("/checkOrder")
    public ResultMsg checkOrder(@RequestBody BookingOrder bookingOrder){
        if (bookingOrder.getScenicId() == null){
            throw new BusinessException(BusinessExceptionEnum.SCENIC_ID_IS_NULL);
        }
        if (bookingOrder.getBookingTime() == null){
            throw new BusinessException(BusinessExceptionEnum.BOOK_TIME_ISNULL);

        }
        orderService.checkOrder(bookingOrder);
        return ResultMsg.successResult();
    }

    @PostMapping("/list")
    @ResponseBody
    public Pagination selectOrderList(@RequestHeader("token") String token, @RequestBody BookingOrder bookingOrder, @RequestHeader("pageNum") String pageNum, @RequestHeader("pageSize") String pageSize) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return PageUtils.getPagination(orderService.selectOrderList(token,bookingOrder));
    }

    @PutMapping("/pay")
    public ResultMsg payOrder(@RequestBody BookingOrder bookingOrder) {
        orderService.payOrder(bookingOrder);
        return ResultMsg.successResult();
    }
}
